package modules.lobby.data
{
	import com.netease.protobuf.UInt64;
	
	import communication.CertManager;
	
	import protobuf.TNMT_BASE_INFO;
	import protobuf.TNMT_STATE_TYPE;


	/**
	 * 锦标赛基本数据模型
	 * @author xumin.xu
	 */
	public class TournamentModel
	{

		/**
		 *开始时间
		 */
		[Bindable]
		public var beginTime:uint = 0;

		[Bindable]
		public var startTimeStr:String = "";

		/**
		 *名称
		 */
		[Bindable]
		public var tournamentName:String;

		/**
		 * 参赛费
		 */
		[Bindable]
		public var buyInAmount:Number = 0;

		/**
		 *手续费
		 */
		[Bindable]
		public var poundage:Number = 0;

		/**
		 *已注册人数
		 */
		[Bindable]
		public var currentPlayer:uint = 0;

		/**
		 *所允许最大参赛人数
		 */
		[Bindable]
		public var maxPlayer:uint = 0;

		/**
		 *比赛状态
		 */
		[Bindable]
		public var _state:int = 0;
		
		public var rebuy:Boolean;
		public var late_reg:Number;

		public function set state(value:int):void
		{
			_state = value;
			tableColor = 0xffffff;
			switch (_state)
			{
				case TNMT_STATE_TYPE.TOURNAMENT_STATE_OPEN_FOR_REG:
					stateStr = TournamentStatus.REGISTERING;
					tableColor = 0x30ff00;
					if (_isRegister)
					{
						stateStr = TournamentStatus.ALREADY_REGISTERED;
						tableColor = 0xffe400;
					}
					break;
				case TNMT_STATE_TYPE.TOURNAMENT_STATE_PLAYING:
					stateStr = TournamentStatus.RUNNING;
					break;
				case TNMT_STATE_TYPE.TOURNAMENT_STATE_CLOSING:
					stateStr = TournamentStatus.COMPLETED;
					break;
			}
		}

		public function get state():int
		{
			return _state;
		}

		/**
		 * 比赛状态字符串
		 */
		[Bindable]
		public var stateStr:String = TournamentStatus.REGISTERING;

		/**
		 *模式
		 */
		[Bindable]
		public var mode:uint = 0;

		/**
		 *锦标赛编号
		 */
		public var tournamentId:UInt64;

		[Bindable]
		public var tableColor:uint = 0x00ff00;

		/**
		 * 是否注册该锦标赛
		 */
		public var _isRegister:Boolean = false;

		public function set isRegister(value:Boolean):void
		{
			_isRegister = value;
			tableColor = 0xffffff;
			if (_state == TNMT_STATE_TYPE.TOURNAMENT_STATE_OPEN_FOR_REG)
			{
				if (_isRegister)
				{
					stateStr = TournamentStatus.ALREADY_REGISTERED;
					tableColor = 0xffe400;
				}
				else
				{
					stateStr = TournamentStatus.REGISTERING;
					tableColor = 0x30ff00;
				}
			}
		}

		public function get isRegister():Boolean
		{
			return _isRegister;
		}

		public var ip:String;

		public var port:uint;
		
		/**
		 *  //1:scheduled 2: sit and go
		 */		
		 [Bindable]
		public var type:uint;
		
		/** 币种 */
		public var currency:String;

		public function update(tournamentBaseInfo:TNMT_BASE_INFO):void
		{
			if (beginTime != tournamentBaseInfo.beginTime)
			{
				_isRegister = false;
			}
			beginTime = tournamentBaseInfo.beginTime; //开始时间
			tournamentName = tournamentBaseInfo.tournamentName; //类型 mtt  scheduled
			buyInAmount = tournamentBaseInfo.buyInAmount; //参赛费
			poundage = tournamentBaseInfo.charge; //手续费
			currentPlayer = tournamentBaseInfo.currentPlayer; //已注册人数
			maxPlayer = tournamentBaseInfo.maxPlayer; //所允许最大参赛人数
			state = tournamentBaseInfo.state; //比赛状态
			mode = tournamentBaseInfo.mode; //模式
			
			if(tournamentBaseInfo.hasRebuy)
			{
				rebuy = tournamentBaseInfo.rebuy;
			}
			
			if(tournamentBaseInfo.hasLateReg)
			{
				late_reg = tournamentBaseInfo.lateReg;
			}
			
			
			ip = CertManager.getInstance().convertByteArrayToString(tournamentBaseInfo.ip);
			port = tournamentBaseInfo.port;
			type = tournamentBaseInfo.type;
			
			currency = CertManager.getInstance().convertByteArrayToString(tournamentBaseInfo.currency);
			
			tournamentId = tournamentBaseInfo.tournamentId;

			startTimeStr = getTimeStr(beginTime * 1000.0);
		}

		/**
		 * 计算时间
		 **/
		public function getTimeStr(time:Number):String
		{
			var date:Date = new Date();
			date.time = time;

//			var year:String = date.fullYear.toString();
			var month:String = (date.getMonth()+1).toString();
			var day:String = date.getDate().toString();
			var hour:String = date.getHours().toString();
			var mimutes:String = date.getMinutes().toString();
			var seconds:String = date.getSeconds().toString();

			if (month.length < 2)
				month = "0" + month;
			if (day.length < 2)
				day = "0" + day;
			if (hour.length < 2)
				hour = "0" + hour;
			if (mimutes.length < 2)
				mimutes = "0" + mimutes;
			if (seconds.length < 2)
				seconds = "0" + seconds;

			return month + "-" + day + " " + hour + ":" + mimutes + ":" + seconds;
		}
	}
}
